define(
	[
		'require',
		'./performance_ctrl'
	],
	function(require){
		function actionFormatter(value, row, index) {

	        return '<div class="" style="text-align:left;" title="'+value+'">'+APM.util.escape(value)+'</div>';
	    }
	    var numData = [];
		var Controller = require('./performance_ctrl').extend({
			formTitle: "最慢交易排行",
			arrayData: Ember.A(['responseTime']),

			formTopArray: Ember.A([{"name":"Top10","id":"10"},{"name":"Top50","id":"50"},{"name":"Top100","id":"100"}]),

			columns: [
			  [  {
			    "field": "channelName",
			    "title": "渠道",
			    formatter:actionFormatter,
			    "colspan": 1,
			    "rowspan": 1
			    // "sortable": true

			  }, 
			  {
			    "field": "responseTime",
			    "title": "平均响应时间（ms）",
			    "colspan": 1,
			    "rowspan": 1
			  },{
			  	 "field": "responseTime_min",
			    "title": "最小(ms)",
			    "colspan": 1,
			    "rowspan": 1
			  },{
			  	 "field": "responseTime_max",
	
			    "title": "最大(ms)",
			    "colspan": 1,
			    "rowspan": 1
			  },{
			  	 "field": "count",
			    "title": "总交易量",
			    "colspan": 1,
			    "rowspan": 1
			  },{
			  	 "field": "apdex_satisfied",
			    "title": "体验好的次数\n<1T",
			    "colspan": 1,
			    "rowspan": 1
			  },{
			  	 "field": "apdex_tolerate",
			    "title": "可容忍的次数\n1T-4T",
			    "colspan": 1,
			    "rowspan": 1
			  },{
			  	 "field": "apdex_unsatisfied",
			    "title": "慢交易数\n>4T",
			    "colspan": 1,
			    "rowspan": 1
			  }],
			],
			url:"report/trade/slow/download",

			//根据数据来填充表格里的内容
			fillTableContent: function(node,index){
				$(".table-left-container .tbody").append("<div class='table-left-tr clearfix'></div>");
				var tableTr = $(".table-left-container .tbody .table-left-tr").eq(index);
				var num = ((node.channel.length+1)*50);
				if(index == 0){
					num = num - 1;
				}
				$(".table-left-container .tbody .table-left-tr").eq(index).css({"height":num,"line-height":num+'px'});
				if(node.typeName){
					$("<div class='table-left-td'><div class='word-break'>"+node.typeName+"</div></div>").appendTo(tableTr);
				}else{
					$("<div class='table-left-td'>-</div>").appendTo(tableTr);
				}
				if(node.applicationName){
					$("<div class='table-left-td'><div class='word-break'>"+node.applicationName+"</div></div>").appendTo(tableTr);
				}else{
					$("<div class='table-left-td'>-</div>").appendTo(tableTr);
				}

				if(node.serviceName){
					$("<div class='table-left-td'><div class='word-break'>"+node.serviceName+"</div></div>").appendTo(tableTr);
				}else{
					$("<div class='table-left-td'>-</div>").appendTo(tableTr);
				}
				$(".table-left-container .tbody .table-left-td").css("width","33%");
				$(".table-left-container .tbody .table-left-td").eq(2).css("width","34%");
				$(".table-left-container .tbody .table-left-tr").eq(index).find(".word-break").css("marginTop",(num-$(".word-break").eq(index).height())/2);
			},
			//根据数据插入表格头部
			fillTableHeader: function(){
				$(".table-left-container .thead").find("div").remove();
				$(".table-left-container .thead").append("<div class='clearfix table-left-tr'></div>");
				var tableTr = $(".table-left-container .thead .table-left-tr");

				$("<div class='table-left-td pull-left'>交易名称</div>").appendTo(tableTr);
				$("<div class='table-left-td pull-left'>业务系统</div>").appendTo(tableTr);
				$("<div class='table-left-td pull-left'>服务节点</div>").appendTo(tableTr);
				$(".table-left-container .thead .table-left-td").css("width","33%");
				$(".table-left-container .thead .table-left-td").eq(2).css("width","34%");
				$(".table-left-container .thead .table-left-tr").css({"margin-left":"1px"});
				// setTimeout(function(){
				// 	$(".fixed-table-container thead th .th-inner").addClass("active");
				// },0)
				$(".table-left-container .thead").css({"height":"40px","line-height":"40px"});
				
			},
			formTopValueChange: Ember.observer("formTopValue",function(){
				this.reportformData();
			}),
			formTopValue:10,
			reportformData: function(){
				var ctrl = this;
				var params = {
					"startDate": +this.get("startDate"),
					"endDate": +this.get("endDate"),
					"top": ctrl.get("formTopValue"),
					// "startDate": 1471164780000,
					// "endDate": 1472958060000
				}
				clearTimeout(ctrl.get("clearTime"));
				
				var newArray = [],
					newObject;

				ctrl.set("tipTitle","");
				numData = [];
				return this.store.ajax("GET", "report/trade/slow",params)
				.then(function(response){
					var performances = response.performances;

					//图表数据数据
					var chartsData = [];
					if(performances[0]){
						ctrl.fillTableHeader();
						$(".table-left-container .tbody").find("div").remove();

						performances.forEach(function(node,index){
							
							ctrl.fillTableContent(node,index);

							newArray.push(node);

							numData.push(node["channel"].length+1);

							newObject = Ember.merge(node,{"channelName":"合计"});

							node.channel.forEach(function(app){

								newArray.push(Ember.merge(app,{"applicationName":node.applicationName,"serviceName":node.serviceName,"typeName":node.typeName}));
							});
						});
					}else{
						ctrl.set("tipTitle","这段时间内暂无数据");
						$(".table-left-container .tbody").find("div").remove();
						$(".table-left-container .tbody").append("<div class='table-left-tr clearfix'></div>");
						$(".table-left-container .tbody .table-left-tr").css("height","49px");
					
						ctrl.fillTableHeader();
						chartsData = [];
					}

					

					newArray.forEach(function(node,index){
						if(node["channelName"] == "合计"){
							node["typeName"] = node["typeName"] +"@"+ node["applicationName"] +"@"+ node["serviceName"];
							chartsData.push(node);
						}
					});
					ctrl.set("chartsData",chartsData);
					ctrl.set("groupData",newArray);
					var j = 0;
					setTimeout(function(){
						numData.forEach(function(node,index){
							if(index%2 == 0){
								for(var i=0;i<node;i++){

									$(".fixed-table-container tbody tr").eq(j+i).find("td").css("background"," rgba(0,0,0,.25)");
									
								}
							
							}else{
								for(var i=0;i<node;i++){
									$(".fixed-table-container tbody tr").eq(j+i).find("td").css("background"," rgba(0,0,0,.12)");
								}
							}
							j = j+node;
						});
						var width = $(".table-wrapper").width();
						$(".fixed-table-container tbody .no-records-found td").css({"textAlign":"left"}).css({"padding-left":(width/2-(width*0.25))-24 + "px"});
					},0);
					
				});
			}
		});

		APM.FormSlowestController = Controller;
		return Controller;
	}
);